// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import '../static/js/validate.js'


import 'nprogress/nprogress.css'
import 'element-ui/lib/theme-chalk/index.css'

import Vue from 'vue'
import router from './router/index.js'
import store from './store/index.js'
import App from './App'
import NProgress from 'nprogress'
import axios from 'axios'
import qs from 'qs'
import { Message, Pagination } from 'element-ui';
// import VueHighlightJS from 'vue-highlightjs'
import hljs from 'highlight.js';
import 'highlight.js/styles/vs.css'

Vue.directive('hljs', el => {
  let blocks = el.querySelectorAll('pre code');
  Array.prototype.forEach.call(blocks, hljs.highlightBlock);
});





Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})
router.afterEach(() => {
  NProgress.done()
})
//注册组件
Vue.component(Message.name, Message)
Vue.component(Pagination.name, Pagination)
// // 告诉Vue 要使用插件 vue-highlightjs
// Vue.use(VueHighlightJS)

//配置全局快捷变量
Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;
Vue.prototype.$message = Message;
Vue.prototype.$pagination = Pagination;
Vue.prototype.$store = store;


//axios.defaults.headers.common['Authorization'] = store.state.token;
// 添加请求拦截器
axios.interceptors.request.use(_config => {
  // 在发送请求之前做些什么
  //判断是否存在token，如果存在将每个页面header都添加token
  if (store.state.token && _config.url != config.mryjHostUrl) {
    _config.headers.common['Authorization'] = store.state.token
  }
  return _config
}, error => {
  return Promise.reject(error);
})
// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    console.log(error)
    switch (error.response.status) {
      case 401://token过期，清除它,清除token信息并跳转到登录页面
        store.commit('del_token');
        return;
      case 403://权限
        store.commit('del_token');
        return;
    }
    return Promise.reject(error.response.data)
  })

/* eslint-disable no-new */
new Vue({
  router,
  template: '<App></App>',
  components: { App }
}).$mount('#app')

